﻿using RabbitMQ.Client;
using RabbitMQ.Extension;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory//创建连接工厂对象
            {
                HostName = "192.168.238.129",//IP地址
                Port = 5672,//端口号
                UserName = "admin",//用户账号
                Password = "123",//用户密码
                AutomaticRecoveryEnabled = true,
                TopologyRecoveryEnabled = true,
                RequestedConnectionTimeout = TimeSpan.FromSeconds(2),
                NetworkRecoveryInterval = TimeSpan.FromSeconds(5)
            };

            //var hosts = new string[] { "192.168.1.1", "192.168.1.2" }; //集群
            var conn = new RabbitConnection(factory);
            conn.OnError(ex =>
            {
                Console.WriteLine(ex.Message);
            });

            conn.OnConnect(() =>
            {
                Console.WriteLine("连接成功");
            });

            conn.OnClose(() =>
            {
                Console.WriteLine("连接断开");
            });
            conn.Open();

            TestWork(conn);
            TestPubSub(conn);
            TestRouting(conn);
            TestTopic(conn);
            Console.ReadKey();
        }


        private static void TestWork(RabbitConnection conn)
        {
            var producer = conn.CreateProducer(RabbitType.Work);
            producer.Bind("test");
            Task.Run(() =>
            {
                while (true)
                {
                    var data = Encoding.UTF8.GetBytes("Work");
                    producer.TryPublish(data);
                    Thread.Sleep(1000);
                }
            });

            var consumer = conn.CreateConsumer(RabbitType.Work);
            consumer.Bind("test");
            consumer.OnReceived((data, id, key) =>
            {
                var d = Encoding.UTF8.GetString(data);
                Console.WriteLine(d);
            });

            //get message by your
            //var msg = consumer.GetMessage();
            //var d = Encoding.UTF8.GetString(msg.Item1);

        }

        private static void TestPubSub(RabbitConnection conn)
        {
            var queue = "";
            var producer = conn.CreateProducer(RabbitType.SubPub);
            producer.Bind(queue, "exchange-1");
            Task.Run(() =>
            {
                while (true)
                {
                    var data = Encoding.UTF8.GetBytes("PubSub");
                    producer.TryPublish(data);
                    Thread.Sleep(1000);
                }
            });

            var consumer = conn.CreateConsumer(RabbitType.SubPub);
            consumer.Bind(queue, "exchange-1");
            consumer.OnReceived((data, id, key) =>
            {
                var d = Encoding.UTF8.GetString(data);
                Console.WriteLine(d);
            });
        }

        private static void TestRouting(RabbitConnection conn)
        {
            var producer = conn.CreateProducer(RabbitType.Routing);
            producer.Bind("", "exchange-2");
            Task.Run(() =>
            {
                while (true)
                {
                    var data = Encoding.UTF8.GetBytes("Routing-a");
                    producer.TryPublish(data, "a");
                    var data2 = Encoding.UTF8.GetBytes("Routing-b");
                    producer.TryPublish(data, "b");
                    Thread.Sleep(1000);
                }
            });

            var consumer = conn.CreateConsumer(RabbitType.Routing);
            consumer.Bind("", "exchange-2", new List<string> { "a", "b" });
            consumer.OnReceived((data, id, key) =>
            {
                var d = Encoding.UTF8.GetString(data);
                Console.WriteLine(d + "===="+key);
            });
        }

        private static void TestTopic(RabbitConnection conn)
        {
            var producer = conn.CreateProducer(RabbitType.Topic);
            producer.Bind("", "exchange-3");
            Task.Run(() =>
            {
                while (true)
                {
                    var data = Encoding.UTF8.GetBytes("主题a");
                    producer.TryPublish(data, "t.aa");
                    var data2 = Encoding.UTF8.GetBytes("主题b");
                    producer.TryPublish(data2, "t.bb");
                    Thread.Sleep(1000);
                }
            });

            var consumer = conn.CreateConsumer(RabbitType.Topic);
            consumer.Bind("", "exchange-3", new List<string> { "t.#" });
            consumer.OnReceived((data, id, key) =>
            {
                var d = Encoding.UTF8.GetString(data);
                Console.WriteLine(d + "====" + key);
            });
        }
    }
}
